const transformationDefault = {
  id: '',
  name: 'ddd',
  description:'',
  status: 0,
  version: '1.0',
  items: [],
  links: [],
  config:{
    schedul:{
      exeType: 0,/** 执行时间类型：0 非定时任务 1 单次执行  2 间隔执行，3 每天执行，4 每周执行，5 每月执行，6 core表达式。 */
      executetime: "",
      week: 1,
      day: 1,
      hour: 1,
      minute:1,
      hournum:0,
      minnum:0,
      secondnum:0,
      startTime:null,
      stopTime:null,
      cronExpression:"",
      concurrent:false
    },
    datax:{
      setting:{
        keyVersion:"",
        speed:{
          channel:5
        },
        errorLimit:{

        }
       },
      model:"standalone"
    }  
},


}
export default class Job {
  constructor(transProp = transformationDefault) {
    this.id = transProp.id || transformationDefault.id
    this.type = transProp.type || transformationDefault.type
    this.name = transProp.name || transformationDefault.name
    this.description = transProp.description || transformationDefault.description
    this.status = transProp.status || transformationDefault.status
    this.version = transProp.version || transformationDefault.version
    this.items = transProp.items || transformationDefault.items
    this.links = transProp.links || transformationDefault.links
    this.config = transProp.config || transformationDefault.config
    // this.setting = transProp.setting || transformationDefault.setting
    // this.model = transProp.model || transformationDefault.model
  }
  /**
   * 批量添加Step
   * @param {*} stepList Step列表
   */
  addSteps(stepList) {
    this.items = [...this.items, ...stepList]
  }
  /**
   * 获取Step
   * @param {*} stepId StepId
   */
  getStepById(stepId) {
    return this.items.find(item => item.id == stepId)
  }
  /**
   * 获取转换中的所有Step
   * @param {*} except 排除的StepId
   * @returns {id:'',name:''} 包含StepId和StepName的对象列表
   */
  getAllStep(except) {
    const resList = []
    const filter = this.items.filter(item => item.id !== except)
    for (const step of filter) {
      resList.push({
        id: step.id,
        name: step.name
      })
    }
    return resList
  }
  /**
   * 获取连线到Step的上一步
   * @param {*} stepId 当前Step的Id
   * @returns [ids] 上一步Step的ID的列表
   */
  getPreStepIds(stepId) {
    const links = this.getLinkToStepById(stepId)
    const ids = []
    for (const link of links) {
      // 连线失效不允许获取上一步的数据
      if (link.enabled) {
        ids.push(link.fromId)
      }
    }
    return ids
  }
  /**
   * 获取连线到Step的上一步
   * @param {*} stepId 当前Step的Id
   * @returns [step] 上一步Step的列表
   */
  getPreStep(stepId) {
    const ids = this.getPreStepIds(stepId)
    const steps = []
    for (const id of ids) {
      steps.push(this.getStepById(id))
    }
    return steps
  }
  /**
   * 获取连线到Step的前面的所有Step
   * @param {*} stepId 当前Step的Id
   * @returns[ids] 连线到Step的前面的所有Step的id列表
   */
  getAllPreStepIds(stepId) {
    // 获取前一步IDS
    let ids = this.getPreStepIds(stepId)
    for (const id of ids) {
      ids = [...ids, ...this.getAllPreStepIds(id)]
    }
    return [...(new Set(ids))]
  }
  /**
   * 获取连线到Step的前面的所有Step
   * @param {*} stepId 当前Step的Id
   * @returns[ids] 连线到Step的前面的所有Step的列表
   */
  getAllPreStepNameAndIds(stepId) {
    const ids = this.getAllPreStepIds(stepId)
    return this.getAllStep(stepId).filter(step => ids.includes(step.id))
  }
  /**
   * 移除Step
   * @param {*}  stepId： 当前Step的Id
   */
  removeStep(stepId) {
    const index = this.items.findIndex(element => element.id == stepId)
    this.items.splice(index, 1)
    // 清除步骤相关连线
    let linkIndex = null
    do {
      linkIndex = this.links.findIndex(element => element.fromId == stepId || element.toId == stepId)
      if (linkIndex !== -1) {
        this.links.splice(linkIndex, 1)
      }
    }
    while (linkIndex > -1)
  }
  /**
   * 添加连线
   * @param {*} links： 连线列表
   */
  addLinks(links) {
    this.links = [...this.links, ...links]
  }
  /**
   * 获取连线
   * @param {*} link： 连线
   */
  getLink(link) {
    return this.links.find(item => item.fromId == link.fromId && item.toId == link.toId)
  }
  /**
   * 删除连线
   * @param {*} link：连线
   */
  removeLink(link) {
    const index = this.links.findIndex(element => element.fromId == link.fromId && element.toId == link.toId)
    this.links.splice(index, 1)
  }
  /**
   * 获取转换中所有连线
   */
  getAllLinks() {
    return this.links
  }
  /**
   * 获取连接到Step的所有连线
   * @param {*} stepId：StepId
   * @returns [link] link列表
   */
  getLinkToStepById(stepId) {
    return this.links.filter(link => link.toId == stepId)
  }
  /**
   * 获取从Step拉出的所有连线
   * @param {*} stepId：StepId
   * @returns [link] link列表
   */
  getLinkFormStepById(stepId) {
    return this.links.filter(link => link.fromId == stepId)
  }
  /**
   * 获取从Step拉出的所有连线的TargetStep
   * @param {*} stepId：StepId
   * @returns [link] link列表
   */
  getTargetStepsFormStepById(stepId) {
    const links = this.getLinkFormStepById(stepId)
    const toIds = links.map(link => link.toId)
    return this.items.filter(item => toIds.includes(item.id))
  }
}
